<?php

/**
 * Facebook Plugin
 * A generic Facebook plugin for CakePHP : <http://www.cakephp.org/>
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright (c)	2009, Michael DePuy.
 * @link			http://www.apswerve.com
 * @package			facebook-plugin
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class FacebookController extends FacebookAppController {
	var $name = 'Facebook';
	
	// Model
	var $uses = array('FacebookController',FACEBOOK_DEFAULT_PRODUCT,'FacebookAction');
	
	// Components
	var $components = array('BrightcoveClient');
	
	/** 
	 * Name: beforeFilter 
	 * Desc: Make sure users are logged into facebook 
	 */  
	function beforeFilter() {  
		$this->user = $this->facebook->require_login();  
		$this->BrightcoveClient->driver = $this->echove;
	} 
	
	function index() {
		$this->set('user', $this->user);
		
		//$this->facebook->api_client->profile_setInfo(null, $this->user, 'info', null, null, 'this is info text');
		
		$this->facebook->api_client->profile_setFBML(null, $this->user, 'profile', null, null, 'this is profile text');
		// Retrieve the user's friends and pass them to the view.
		$friends = $this->facebook->api_client->friends_get(); 
		$this->set('friends', $friends);
		
		if(class_exists(FACEBOOK_DEFAULT_PRODUCT)) {
			$model_name = FACEBOOK_DEFAULT_PRODUCT;
			$this->set('product_model_name', $model_name);
			$product = new $model_name;
			$all = $product->findAll();
			$this->set('browse_products', $all);
			// Create an array of popular products
			$popular_products = array();
			foreach($all as $p) {
				if($p[$model_name]['is_popular']==1) {
					array_push($popular_products, $p);
				}
				// Make this the featured product if is marked as such
				if($p[$model_name]['is_featured']==1) {
					$this->set('featured_product', $p);
				}
			}
			$this->set('popular_products', $popular_products);
			
			$this->set('recent_videos', $this->BrightcoveClient->getRecentlyAdded(10));
			
		} else {
			echo "does not exist";
		}
	}
	
	/**
	 * Show the about page.
	 * @return 
	 */
	function about() {
		$this->set('selected_menu', "about");
		
	}
	
}
	
?>